[Home] Python으로 돌아가기

[박주민][파이썬과 아두이노] 9. Matplotlib을 활용한 그래프 시각화

[YouTube]

https://www.youtube.com/watch?v=4-j_0CtZ1xw

[참고 자료]

[범례 자료] Matplotlib Legend API

[마커 자료] Matplotlib Markers API

[스타일 자료 1] Matplotlib Plot Styles

[스타일 자료 2] Matplotlib Line Styles

[Code]

9.그래프.py [Download]

[데이터] 대전 날씨.xlsx [Data Source]

[예제 코드 1] 기본 그래프


import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [10, 15, 7, 12, 20]

plt.plot(x, y, marker='o', linestyle='-', color='b', label='기본 그래프')
plt.xlabel('X축')
plt.ylabel('Y축')
plt.title('기본 그래프')
plt.legend()
plt.show()
    

[예제 코드 2] 그래프 범례


plt.plot(x, y, marker='o', linestyle='-', color='r', label='데이터 1')
plt.plot(x, [5, 10, 15, 10, 5], marker='s', linestyle='--', color='g', label='데이터 2')
plt.xlabel('X축')
plt.ylabel('Y축')
plt.title('범례가 포함된 그래프')
plt.legend()
plt.show()
    

[예제 코드 3] 그래프 스타일


plt.plot(x, y, marker='^', linestyle=':', color='purple', label='점선 스타일')
plt.xlabel('X축')
plt.ylabel('Y축')
plt.title('스타일 적용된 그래프')
plt.legend()
plt.show()
    

[예제 코드 4] 서브 플롯


fig, axes = plt.subplots(2, 1, figsize=(6, 8))

axes[0].plot(x, y, marker='o', color='b', label='그래프 1')
axes[0].set_title('서브 플롯 1')
axes[0].legend()

axes[1].plot(x, [20, 12, 7, 15, 10], marker='s', color='r', label='그래프 2')
axes[1].set_title('서브 플롯 2')
axes[1].legend()

plt.tight_layout()
plt.show()
    

[추가 자료]

추가 자료 1 추가 자료 2 추가 자료 3 추가 자료 4